﻿using IBatisNet.DataMapper;
using System.Collections.Generic;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using System;
using System.Data;
using JZ.JiangSu.Model.Entity;

namespace JZ.JiangSu.Repository
{
    public class BatisRepository<T> where T : BaseEntity
    {
        public int Insert(string statementName, T t)
        {
            var iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
                return (int?)iSqlMapper.Insert(statementName, t) ?? 0;
            return 0;
        }
        public int Update(string statementName, T t)
        {
            var iSqlMapper = Mapper.Instance();
            return iSqlMapper != null ? iSqlMapper.Update(statementName, t) : 0;
        }

        public int Update(string statementName, object parameterObject = null)
        {
            var iSqlMapper = Mapper.Instance();
            return iSqlMapper != null ? iSqlMapper.Update(statementName, parameterObject) : 0;
        }

        public int Delete(string statementName, object primaryKeyId)
        {
            var iSqlMapper = Mapper.Instance();
            return iSqlMapper != null ? iSqlMapper.Delete(statementName, primaryKeyId) : 0;
        }

        public T Get(string statementName, object primaryKeyId)
        {
            var iSqlMapper = Mapper.Instance();
            return iSqlMapper != null ? iSqlMapper.QueryForObject<T>(statementName, primaryKeyId) : default(T);
        }

        public IList<T> QueryForList(string statementName, object parameterObject = null)
        {
            var iSqlMapper = Mapper.Instance();
            return iSqlMapper != null ? iSqlMapper.QueryForList<T>(statementName, parameterObject) : null;
        }
        //QueryForObject
        public int QueryForObject(string statementName, object parameterObject = null)
        {
            var iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
                return (int?)iSqlMapper.QueryForObject(statementName, parameterObject) ?? 0;
            return 0;
        }

        /// <summary>
        /// 得到运行时ibatis.net动态生成的SQL
        /// </summary>
        /// <param name="sqlMapper"></param>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public Tuple<string, IDbDataParameter[]> GetRuntimeSql(string statementName, T t)
        {
            var iSqlMapper = Mapper.Instance();
            string result = string.Empty;
            //var sqlpara = new List<IDbDataParameter>();
            IDbDataParameter[] para = null;
            try
            {
                IMappedStatement statement = iSqlMapper.GetMappedStatement(statementName);
                if (!iSqlMapper.IsSessionStarted)
                {
                    iSqlMapper.OpenConnection();
                }
                RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, t, iSqlMapper.LocalSession);
                result = scope.PreparedStatement.PreparedSql;
                para = scope.PreparedStatement.DbParameters;
                //dd[3].
            }
            catch (Exception ex)
            {
                result = "获取SQL语句出现异常:" + ex.Message;
            }
            return new Tuple<string, IDbDataParameter[]>(result, para);
            //return result;
        }

        public int Insert(string statementName, object t)
        {
            var iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
                return (int?)iSqlMapper.Insert(statementName, t) ?? 0;
            return 0;
        }
    }
}
